package level.editor.control.tile;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JButton;

import level.model.Tile;

public class TileButton extends JButton
{
	public TileButton(Icon icon)
	{
		super("", icon);
		setBorderPainted(false);
		setContentAreaFilled(false);
		tile = new Tile();
		setFocusPainted(false);
		setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
	}

	public void setSx(int x)
	{
		tile.setSx(x);
	}

	public void setSy(int y)
	{
		tile.setSy(y);
	}

	public void setIconPath(String fileName)
	{
		tile.setIconPath(fileName);
	}

	public Tile getTile()
	{
		return tile;
	}

	public void selected(boolean b)
	{
		selected = b;
		repaint();
	}

	@Override
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		if (selected)
			paintSelected(g);
	}

	private void paintSelected(Graphics gOld)
	{
		Graphics2D g = (Graphics2D) gOld;
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		g.setColor(Color.BLACK);
		g.setStroke(new BasicStroke(3f));

		g.draw(new Rectangle(0, 0, getSize().width - 1, getSize().height - 1));
	}

	private Tile tile;
	private boolean selected = false;
}
